package com.lydms.service.impl;

import com.lydms.dao.EnglishDao;
import com.lydms.pojo.English;
import com.lydms.pojo.EnglishQuery;
import com.lydms.pojo.EnglishQuery.Criteria;
import com.lydms.service.WordSelectService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class WordSelectServiceImpl implements WordSelectService {
    private static final Logger logger = LogManager.getLogger(WordSelectServiceImpl.class);

    @Autowired
    private EnglishDao englishDao;

    @Value(value = "${emailCategory}")
    private int emailCategory;


    /**
     * 根据英文单词查询单个数据
     *
     * @param english
     * @return
     */
    @Override
    public English selectOneByEnglish(String english) {
        EnglishQuery query = new EnglishQuery();
        Criteria criteria = query.createCriteria();
        criteria.andEnglishEqualTo(english);
        List<English> en = englishDao.selectByExample(query);
        if (en.size() == 0) {
            logger.info("从数据库返回的结果为null");
            return null;
        } else {
            English result = en.get(0);
            logger.info("查询到的结果为：{}", result);
            return result;
        }
    }


    /**
     * 查询分类2，指定天的数据（未发则发送今天的数据）
     *
     * @param day
     * @return
     */
    @Override
    public List<English> selectToday(String day) {
        logger.info("查询分类{}的日期为:{}", emailCategory, day);
//        获取查询的天数
        if (day == null) {
            SimpleDateFormat mm = new SimpleDateFormat("dd");
            Date date = new Date();
            day = mm.format(date);
        }
        int now = Integer.parseInt(day);
//        查询当前数据
        EnglishQuery query = new EnglishQuery();
        Criteria criteria = query.createCriteria();
        criteria.andCategoryEqualTo(emailCategory);
        criteria.andDayEqualTo(now);
        List<English> englishList = englishDao.selectByExample(query);
        logger.info("查询出来的结果为:{}", englishList.toString());
        return englishList;
    }
}
